-- Block 1. Basic lsp setup
local jdtls = require('jdtls')

local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')

local home = os.getenv('HOME')

local workspace_dir = home .. '/.eclipse-nvim/' .. project_name

local cmp = require('cmp')

local dap = require('dap')

local dap_widgets = require('dap.ui.widgets')

cmp.setup({
	snippet = {
		-- REQUIRED - you must specify a snippet engine
		expand = function(args)
			vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
			-- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
			-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
			-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
		end,
	},
	window = {
		completion = cmp.config.window.bordered(),
		documentation = cmp.config.window.bordered(),
	},
	mapping = cmp.mapping.preset.insert({
		['<C-b>'] = cmp.mapping.scroll_docs(-4),
		['<C-f>'] = cmp.mapping.scroll_docs(4),
		['<C-Space>'] = cmp.mapping.complete(),
		['<C-e>'] = cmp.mapping.abort(),
		['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
	}),
	sources = cmp.config.sources({
		{ name = 'nvim_lsp' },
		{ name = 'vsnip' }, -- For vsnip users.
		-- { name = 'luasnip' }, -- For luasnip users.
		-- { name = 'ultisnips' }, -- For ultisnips users.
		-- { name = 'snippy' }, -- For snippy users.
	}, {
		{ name = 'buffer' },
	})
})

cmp.setup.cmdline(':', {
	mapping = cmp.mapping.preset.cmdline(),
	sources = cmp.config.sources({
		{ name = 'path' }
	}, {
		{ name = 'cmdline' }
	})
})

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)

function on_attach(client, bufnr)
	--	require('jdtls.dap').setup_dap_main_class_configs()
	dap.defaults.fallback.terminal_win_cmd = 'tabnew'
	--	dap.load_launchjs()
	require('dap.ext.vscode').load_launchjs()
	--	dap.defaults.fallback.focus_terminal = true
end

local config = {
	cmd = {
		'java',
		'-Declipse.application=org.eclipse.jdt.ls.core.id1',
		'-Dosgi.bundles.defaultStartLevel=4',
		'-Declipse.product=org.eclipse.jdt.ls.core.product',
		'-Dlog.protocol=true',
		'-Dlog.level=ALL',
		'-Xmx1g',
		'--add-modules=ALL-SYSTEM',
		'--add-opens', 'java.base/java.util=ALL-UNNAMED',
		'--add-opens', 'java.base/java.lang=ALL-UNNAMED',
		'-jar', home .. '/java/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar',
		'-configuration', home .. '/java/jdtls/config_linux',
		'-data', workspace_dir,
	},
	root_dir = require('jdtls.setup').find_root({ '.git', 'mvnw', 'gradlew', '.root', 'pom.xml' }),
	settings = {
		java = {
			maven = {
				downloadSources = true,
			},
			eclipse = {
				downloadSources = true,
			},
		}
	},
	init_options = {
		bundles = {
			vim.fn.glob(
				home ..
				"/java/nvim-utils/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-0.48.0.jar")
		},
	},
	capabilities = capabilities,
	on_attach = on_attach,
}

function vars()
	--	dap_widgets.centered_float(dap_widgets.scopes)
	local widgets = require('dap.ui.widgets')
	local my_sidebar = widgets.sidebar(widgets.scopes, nil, 'belowright 20split')
	my_sidebar.open()
end

function mvnCleanInstall()
	vim.cmd('tabnew term://mvn clean install')
end

function mvnCleanPackage()
	vim.cmd('tabnew term://mvn clean package')
end

vim.keymap.set('n', '<space>h', vim.lsp.buf.hover)
vim.keymap.set('n', '<space>d', vim.lsp.buf.definition)
vim.keymap.set('n', '<space>i', vim.lsp.buf.implementation)
vim.keymap.set('n', '<space>s', vim.lsp.buf.signature_help)
vim.keymap.set('n', '<space>a', vim.lsp.buf.code_action)
vim.keymap.set('n', '<space>o', jdtls.organize_imports)
vim.keymap.set('n', '<space>r', vim.lsp.buf.rename)
vim.keymap.set('n', '<space>ev', jdtls.extract_variable)
vim.keymap.set('n', '<space>ec', jdtls.extract_constant)
vim.keymap.set('n', '<space>f', vim.lsp.buf.format)
vim.keymap.set('n', '<space>b', dap.toggle_breakpoint)
vim.keymap.set('n', '<F10>', dap.continue)
vim.keymap.set('n', '<F5>', dap.step_over)
vim.keymap.set('n', '<F6>', dap.step_into)
vim.keymap.set('n', '<space>dt', dap.terminate)
vim.keymap.set('n', '<space>dd', dap.disconnect)
vim.keymap.set('n', '<space>dr', dap.repl.toggle)
vim.keymap.set('n', '<space>df', '<cmd>Telescope dap frames<cr>')
vim.keymap.set('n', '<space>dv', vars)
vim.keymap.set('n', '<space>dh', '<cmd>Telescope dap commands<cr>')
vim.keymap.set('n', '<space>mci', mvnCleanInstall)
vim.keymap.set('n', '<space>mcp', mvnCleanPackage)

jdtls.start_or_attach(config)
